package edu.sysu.demo.petstore.dog.user;

import edu.sysu.demo.petstore.dog.data.entity.User;
import java.time.LocalDate;
import java.util.Collection;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

class UserDetailsAdapter implements UserDetails {

  private static final long serialVersionUID = -550244151990992803L;
  private final User user;

  UserDetailsAdapter(User user) {
    this.user = user;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return user.getRoles()
        .stream()
        .map(GrantedAuthorityAdapter::new)
        .collect(Collectors.toList());
  }

  @Override
  public String getPassword() {
    return user.getPassword();
  }

  @Override
  public String getUsername() {
    return user.getUsername();
  }

  @Override
  public boolean isAccountNonExpired() {
    return !user.getExpireAt().isBefore(LocalDate.now());
  }

  @Override
  public boolean isAccountNonLocked() {
    return !user.getLocked();
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return false;
  }

  @Override
  public boolean isEnabled() {
    return user.getEnabled();
  }
}
